const http = require('http');
const https = require('https');
const querystring = require('querystring');

const requestData = querystring.stringify({
  'msg': 'Hello World!'
});

const httpPostOptions = {
  hostname: 'localhost',
  port: 8080,
  path: '/upload',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': Buffer.byteLength(requestData)
  }
};

const options = {
  hostname: 'www-t0.ete.cathaypacific.com',
  port: 443,
  path: '/cx/.dictionary.responsive_mmb_hotel.en.json',
  method: 'GET',
  rejectUnauthorized: false
};

const request = https.request(options, (response) => {
  console.log(`STATUS: ${response.statusCode}`);
  console.log(`HEADERS: ${JSON.stringify(response.headers)}`);

  response.setEncoding('utf8');
  var responseChunks = [];
  response.on('data', (chunk) => {
    console.log(chunk);
    responseChunks.push(chunk);
  });
  response.on('end', () => {
    let responseBody = Buffer.concat(responseChunks).toString();
    console.log(`BODY: ${responseBody}`);
  });
});

request.on('error', (err) => {
  console.error(`Problem with request: ${err.message}`);
});

request.write(requestData);
request.end();